
sysdir set PERSONAL P:\AA_RESEARCHER_TRAINING_INFORMATION\STATA_ADO_PACKAGES
/***************************Can run from here once data set above is saved*******************************/


global dir_path = "P:\Townsend_5609_wages\Cengiz"
global data = "$dir_path/data"
global temp = "$data/temp"
global log = "$dir_path/logfiles" 

global missedevents "D.MW_real> 0  & D.MW_real<.& D.MW>0 & D.MW_real<=0.25"
local treatdef3 "D.MW_real>0.25  & D.MW_real<. & D.MW>0"						// Every real increase in MW that exceed $0.25 as treatment.

global ifclause "`treatdef3'"

cap log close
cap log using "$log/02 Minimum Wage Assign_JT_241112",replace text


use "$data/Y_variable", clear

rename E_pjt count // Used below to determine proportion of workers between old and new minimum wage.
*sort prov date wagebin
rename N_pt pop
egen countall = sum(count),by(prov quarterdate)


/********  Real Minimum Wages, Wage Increases, Treatment  ******/

global Wmax 17 
global Wmin 4



*** SET PANEL VARS ***

egen wagebinsprov = group(wagebins prov)
xtset wagebinsprov quarterdate 




sort wagebinsprov quarterdate  


g wageM25 = wagebins/100

cap drop *MW* 

*g MW = exp(logmw) 
rename mw MW
g DMW = D.MW
gen MW_real=MW/cpi_base_2002*100
*g MW_real = exp(logmw)/(cpi/100) 
g DMW_real = D.MW_real 
*MW is expressed in terms of lower bound of wagebin it falls in.
g MW_realM25 = MW_real - mod(MW_real,0.25)
g DMW_realM25 = D.MW_realM25


********************************************************************************
****************        Find Quintiles by Below Share        *******************
********************************************************************************

*Generates Forward-looking values of the bin lower bound for the minimum
*wage 1-4 quarters into the future

foreach num of numlist 1(1)4{
	gen F`num'MW_realM25 = F`num'.MW_realM25
}

cap drop tagger
egen tagger =tag(prov quarterdate) /* Tags one observation from each permutation
		of the listed variables*/

	****************************************************************************
	*******         Find treatments & Federal & State Increases      ***********
	****************************************************************************
	
	* Following block of code creates a data set with a value of 
	* 0 or 1 for each province/quarterdate combination if min. wage changes
	* and exceeds threshold ($.25)
	
		preserve
		gen alltreat = (D.MW_real>0.25  & D.MW_real<. & D.MW>0) // province/quarterdate combos where the minimum wage change meets the threshold
			assert alltreat == 1 | alltreat==0
			count if alltreat==1
			scalar asd=r(N)
			keep if tagger==1
			keep prov quarterdate alltreat year
			tempfile widedata
			save `widedata', replace
		restore

	****************************************************************************
	************        Find Effective below bins          *********************
	****************************************************************************

*Get below shares & workforce
*eat variable looks at the next four quarters-
*Used to generate share of employment:
* (i) between current MW and MW 1-4 quarters ahead (variables: _below`num'count)
* (ii) below the MW 1-4 quarters ahead (variables: _below`num'count2).
* Note that the final variables (below)

preserve
forval num=1(1)4{
bys prov quarterdate: egen _below`num'count = total(count) if (wagebins)/100<F`num'MW_realM25 & wagebins/100>=MW_realM25 
bys prov quarterdate: egen _below`num'count2 = total(count) if (wagebins)/100<F`num'MW_realM25
by prov quarterdate:  egen below`num'count = max(_below`num'count)
by prov quarterdate:  egen below`num'count2 = max(_below`num'count2)
assert _below`num'count==below`num'count if _below`num'count!=.
assert _below`num'count2==below`num'count2 if _below`num'count2!=.
}

keep if tagger==1
keep prov quarterdate below* pop _below* countall MW
xtset prov quarterdate
forval num=1(1)4{
gen L`num'below`num'count = L`num'.below`num'count
gen L`num'below`num'count2 = L`num'.below`num'count2
gen L`num'below`num'countall = L`num'.countall
}
merge 1:1 prov quarterdate using `widedata', assert(3) nogenerate




********************************************************************************
***********************      Federal Increases           ***********************
********************************************************************************

			cap drop fedincrease
			gen fedincrease=0
			/*Commented out as federal minimum wage isn't relevant - JT
			replace fedincrease = 1 if quarterdate == tq(1980q1) & alltreat==1 & (MW<=3.10001 & MW>=3.0999)
			replace fedincrease = 1 if quarterdate == tq(1981q1) & alltreat==1 & (MW<=3.35001 & MW>=3.3499)
			replace fedincrease = 1 if quarterdate == tq(1990q2) & alltreat==1 & (MW<=3.80001 & MW>=3.7999)
			replace fedincrease = 1 if quarterdate == tq(1991q2) & alltreat==1 & (MW<=4.25001 & MW>=4.2499)
			replace fedincrease = 1 if quarterdate == tq(1996q4) & alltreat==1 & (MW<=4.75001 & MW>=4.7499)
			replace fedincrease = 1 if quarterdate == tq(1997q3) & alltreat==1 & (MW<=5.15001 & MW>=5.1499)
			replace fedincrease = 1 if quarterdate == tq(2007q3) & alltreat==1 & (MW<=5.85001 & MW>=5.8499)
			replace fedincrease = 1 if quarterdate == tq(2008q3) & alltreat==1 & (MW<=6.55001 & MW>=6.5499)
			replace fedincrease = 1 if quarterdate == tq(2009q3) & alltreat==1 & (MW<=7.25001 & MW>=7.2499)
			*/

save `widedata',replace
restore


preserve
use `widedata',clear
keep if alltreat==1
forval num=1(1)4{
replace L`num'below`num'count = 0 if  L`num'below`num'count==. 					// In some cases L`i'belowshare is "." because size of MW increase is not very large.
*assert  L`num'below`num'count2!=. if year>1981
}
gen belowshare = (L1below1count + L2below2count + L3below3count + L4below4count)/(L1below1countall + L2below2countall + L3below3countall + L4below4countall)
replace belowshare = L1below1count/L1below1countall if prov==10 & quarterdate==149
replace belowshare = (L1below1count + L2below2count + L3below3count )/(L1below1countall + L2below2countall + L3below3countall ) if prov==11 & quarterdate==151

*gen belowshare2 = (L1below1count2 + L2below2count2 + L3below3count2 + L4below4count2)/(L1below1countall + L2below2countall + L3below3countall + L4below4countall)
list if belowshare==.
assert belowshare!=. 
*assert belowshare2!=. if year>1981 

cap drop toosmall
gen toosmall = (belowshare<0.02 & belowshare!=.)
if "$numberquant" == ""{
global numberquant = 3
}
xtile overallcountgroup=belowshare if toosmall==0 , n($numberquant)
xtile belowsharegroup = belowshare if overallcountgr>0 & overallcountgr!=. & fedincrease!=1 , n($numberquant)
assert overallcountgroup!=0 & overallcountgroup!=. if year>=1981 & toosmall==0
assert overallcountgroup<=$numberquant if overallcountgr!=.
keep prov quarterdate overallcountgroup alltreat toosmall belowshare /*belowsharegroup*/ fedincrease /*balancedgroup*/ belowsharegroup
save `widedata',replace
save "${data}eventclassification.dta", replace

/* eventclassification */

use "${data}eventclassification.dta", clear

tsfill, full

replace overallcountgr = 0 if overallcountgr==.
replace belowsharegroup = 0 if belowsharegroup==.

foreach vv in L {
	forval num=1(1)12{
		gen `vv'`num'quarterdate = (`vv'`num'.quarterdate!=. & (`vv'`num'.quarterdate<136 |  `vv'`num'.quarterdate>142) )
	}
}
*

foreach vv in F {
	forval num=1(1)19{
		gen `vv'`num'quarterdate = (`vv'`num'.quarterdate!=. & (`vv'`num'.quarterdate<136 |  `vv'`num'.quarterdate>142) )
	}
}
*




gen fullbelowsharegr = overallcountgr

local vv F
forval num=1(1)19{
		replace fullbelowsharegr = fullbelowsharegr * `vv'`num'quarterdate
	}
*

local vv L
forval num=1(1)12{
		replace fullbelowsharegr = fullbelowsharegr * `vv'`num'quarterdate
	}
*

drop F* L*
count if fullbelowshare>0
xtile fullbelowshare=belowshare if fullbelowshare>0 , n($numberquant)
replace fullbelowshare = 0 if fullbelowshare==.
keep prov quarterdate fullbelowshare
save "${data}fullybalancedevents.dta", replace


restore


cap drop _merge
merge m:1 prov quarterdate using "${data}eventclassification.dta", nogenerate
merge m:1 prov quarterdate using "${data}fullybalancedevents.dta", nogenerate
replace overallcountgroup = 0 if overallcountgroup==.
replace belowsharegroup = 0 if belowsharegroup==.
replace fullbelowshare = 0 if fullbelowshare==.
assert overallcountgroup!=0 if alltreat==1 & toosmall==0
sum overallcountgroup,meanonly
local upperquant = r(max)





********************************************************************************
******************     Finding Federal Increases           *********************
********************************************************************************
xtset wagebinsprov quarterdate

compress






********************************************************************************
************************      Correcting Treatments       **********************
********************************************************************************
xtset wagebinsprov quarterdate
forval k = 0/7  {
	cap drop treat_p`k' 
	cap drop _treat_p`k'
	g _treat_p`k'=0
		replace _treat_p`k' = 1 if $ifclause &  (wageM25>=(MW_realM25+`k' ) &  wageM25 < (MW_realM25 +1 +`k' )) & overallcountgroup>0 & fedincrease!=1
	g byte treat_p`k' = ( _treat_p`k' +  L._treat_p`k' + L2._treat_p`k' + L3._treat_p`k')
	replace treat_p`k' = ( _treat_p`k' +  L._treat_p`k' + L2._treat_p`k') if quarterdate==150
	replace treat_p`k' = ( _treat_p`k' +  L._treat_p`k') if quarterdate==149
	replace treat_p`k' = ( _treat_p`k') if quarterdate==148
	cap drop _treat_p`k'
}

forval k = 1/7  {
	cap drop treat_m`k'
	cap drop _treat_m`k'
	g _treat_m`k'=0
		replace _treat_m`k' = 1 if $ifclause & (wageM25<(MW_realM25 - `k' + 1 )  &  wageM25 >= (MW_realM25 -`k'  ))  & overallcountgroup>0 & fedincrease!=1
	g byte treat_m`k' = ( _treat_m`k' + L._treat_m`k' + L2._treat_m`k' + L3._treat_m`k' )
	replace treat_m`k' = ( _treat_m`k' +  L._treat_m`k' + L2._treat_m`k') if quarterdate==150
	replace treat_m`k' = ( _treat_m`k' +  L._treat_m`k') if quarterdate==149
	replace treat_m`k' = ( _treat_m`k') if quarterdate==148
cap drop _treat_m`k'
}  


 cap drop  Dtreat_p0
 g byte Dtreat_p0 = D.treat_p0 

 
 
********************************************************************************
****************          Controls         *************************************
********************************************************************************


********************************************************************************
****************       Disaggregated Controls        ***************************
********************************************************************************



	forval k = 0/5  {
		cap drop cont_p`k' 
		cap drop _cont_p`k'
		g _cont_p`k'=0
		replace _cont_p`k' = 1 if ($missedevents | toosmall==1) &  (wageM25>=(MW_realM25+`k' ) &  wageM25 < (MW_realM25 +1 +`k' ))
		g cont_p`k' = ( _cont_p`k' +  L._cont_p`k' + L2._cont_p`k' + L3._cont_p`k')
		replace cont_p`k' = ( _cont_p`k' +  L._cont_p`k' + L2._cont_p`k')  if quarterdate==150
		replace cont_p`k' = ( _cont_p`k' +  L._cont_p`k' )  if quarterdate==149
		replace cont_p`k' = ( _cont_p`k' )  if quarterdate==148
		cap drop _cont_p`k'

	}

	forval k = 1/5  {
		cap drop cont_m`k'
		cap drop _cont_m`k'
		g _cont_m`k'=0
		replace _cont_m`k' = 1 if ($missedevents | toosmall==1) & (wageM25<(MW_realM25 - `k' + 1)  &  wageM25 >= (MW_realM25 -`k'  ))
		g cont_m`k' = ( _cont_m`k' + L._cont_m`k' + L2._cont_m`k' + L3._cont_m`k' )
		replace cont_m`k' = ( _cont_m`k' +  L._cont_m`k' + L2._cont_m`k')  if quarterdate==150
		replace cont_m`k' = ( _cont_m`k' +  L._cont_m`k' )  if quarterdate==149
		replace cont_m`k' = ( _cont_m`k' )  if quarterdate==148
	cap drop _cont_m`k'
	}	  

 ********************************************************************************
******************     Fully Aggregated Controls        ************************
********************************************************************************	
	
	
	
cap drop _cont_p
g _cont_p = 0
replace _cont_p = 1 if ($missedevents | toosmall==1) &  (wageM25>=(MW_realM25 ) &  wageM25 < (MW_realM25 +1 +${Wmax} ))
g tempcont_p = _cont_p + L._cont_p + L2._cont_p + L3._cont_p

local Tmax3 = ${Tmaxcont} + 3
forval i = 0(1)`Tmax3'{
	if `i' == 0{
	g postcont_p = _cont_p
	}
	else {
	replace postcont_p = postcont_p + L`i'._cont_p
	}
}
cap drop _cont_p

cap drop _cont_m
g _cont_m = 0
replace _cont_m = 1 if ($missedevents | toosmall==1) &  (wageM25<(MW_realM25 - 0)  &  wageM25 >= (MW_realM25 -  ${Wmin} - 0 ))	
g tempcont_m = _cont_m + L._cont_m + L2._cont_m + L3._cont_m
forval i = 0(1)`Tmax3'{
	if `i' == 0{
	g postcont_m = _cont_m
	}
	else {
	replace postcont_m = postcont_m + L`i'._cont_m
	}
}
cap drop _cont_m


compress


**** DEFINE TREATMENT VARIABLES AND LAGS/LEADS *****
*No need for lags but we need to define leads 

xtset wagebinsprov quarterdate   

********************************************************************************
*************************      Leads and Lags           ************************
********************************************************************************



 
foreach j in  4 8 12 16 {
	foreach k in m5 m4 m3 m2 m1 p0 p1 p2 p3 p4 p5 {
		cap drop F`j'treat_`k'
		cap drop L`j'treat_`k'
		cap drop F`j'cont_`k'
		cap drop L`j'cont_`k'
		
		g F`j'treat_`k' = F`j'.treat_`k'
 		g L`j'treat_`k' = L`j'.treat_`k'
		g F`j'cont_`k' = F`j'.cont_`k'
 		g L`j'cont_`k' = L`j'.cont_`k'

		
		replace F`j'treat_`k' = 0 if F`j'treat_`k' == .
 		replace L`j'treat_`k' = 0 if L`j'treat_`k' == .
		replace F`j'cont_`k' = 0 if  F`j'cont_`k'== .
 		replace L`j'cont_`k' = 0 if L`j'cont_`k' == .
		
		
		
	}
} 

*

foreach j in  4 8 12 16 {
	foreach k in m7 m6 p6 p7 {
		cap drop F`j'treat_`k'
		cap drop L`j'treat_`k'

		g F`j'treat_`k' = F`j'.treat_`k'
 		g L`j'treat_`k' = L`j'.treat_`k'

		
		replace F`j'treat_`k' = 0 if F`j'treat_`k' == .
 		replace L`j'treat_`k' = 0 if L`j'treat_`k' == .
		
		
	}
}
*
********************************************************************************
******************      Leads for Agg. Controls          ***********************
********************************************************************************

	foreach k in m p {
		g precont_`k' = F4.tempcont_`k'
		replace precont_`k' =  0 if precont_`k' == .
			
	}
*

foreach k in m p {
		cap drop earlycont_`k'
		g earlycont_`k' = F12.tempcont_`k' + F8.tempcont_`k'


		replace earlycont_`k' = 0 if earlycont_`k' == .

		}
*


replace postcont_p = 0 if postcont_p==.
replace postcont_m = 0 if postcont_m==.

********************************************************************************
********************           Window Variable             *********************
********************************************************************************


foreach pm in p m{
	foreach num of numlist 1(1)7{
		gen window_`pm'`num' =  F12treat_`pm'`num' +  F8treat_`pm'`num' +  F4treat_`pm'`num' + treat_`pm'`num' + L4treat_`pm'`num'  + L8treat_`pm'`num'  + L12treat_`pm'`num'  + L16treat_`pm'`num'  
	}
}

local pm p
local num 0
gen window_`pm'`num' =  F12treat_`pm'`num' +  F8treat_`pm'`num' +  F4treat_`pm'`num' + treat_`pm'`num' + L4treat_`pm'`num'  + L8treat_`pm'`num'  + L12treat_`pm'`num'  + L16treat_`pm'`num'  


*	



	
compress


save "$data/new_y2", replace

log close
